﻿using System;
using System.Linq;
using System.Web.Mvc;

namespace Songhay.Web.Mvc.Extensions
{
    using Models;

    /// <summary>
    /// Extension Methods
    /// for <see cref="System.Web.Mvc.HtmlHelper"/>.
    /// </summary>
    public static partial class HtmlHelperExtensions
    {
        /// <summary>
        /// Renders new-line characters with indentation.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="indentCount">The indent count.</param>
        public static string NewLine(this HtmlHelper helper, byte indentCount = 0)
        {
            var xhtml = Environment.NewLine;
            for(int i = 0; i < indentCount; i++)
            {
                xhtml += "    ";
            }
            return xhtml;
        }

        /// <summary>
        /// Renders Silverlight <c>object</c> element
        /// wrapped in an attribute-adorned <c>div</c>.
        /// </summary>
        /// <param name="helper">The helper.</param>
        /// <param name="model">The model.</param>
        /// <param name="blockId">The <c>id</c> attribute of the wrapping <c>div</c>.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        public static MvcHtmlString SilverlightControlHost(this HtmlHelper helper, SilverlightModel model, string blockId = "SilverlightControlHost", object htmlAttributes = null)
        {
            if(model == null) throw new ArgumentNullException("model", "The Silverlight Model is null.");

            var xhtml = string.Empty;

            var param = new Func<string, object, string>(
                (tagName, tagValue) =>
                {
                    if(tagValue == null) return string.Empty;

                    return helper.NewLine(3) +
                        (new TagBuilder("param")
                            .WithAttributes(new { name = tagName, @value = tagValue })
                            .ToString(TagRenderMode.SelfClosing));
                });

            var historyFrame = new Func<string>(
                ()=>
                {
                    var s = helper.NewLine(2) +
                        (new TagBuilder("iframe")
                            .WithAttributes(new { id="_sl_historyFrame", style="visibility: hidden; height: 0px; width: 0px; border: 0px;" })
                            .ToString(TagRenderMode.Normal));
                    return s;
                });

            var objectTag = new TagBuilder("object")
                .WithAttributes(
                    new
                    {
                        data = "data:application/x-silverlight-2,",
                        type = "application/x-silverlight-2",
                        width = model.Dimensions.Width,
                        height = model.Dimensions.Height
                    }
                )
                .WithInnerHtml(
                    param("source", model.Source) +
                    param("onError", "onSilverlightError") +
                    param("autoUpgrade", true) +
                    param("background", model.BackgroundColor) +
                    param("enablehtmlaccess", model.EnableHtmlAccess) +
                    param("minRuntimeVersion", model.MinimumRuntimeVersion) +
                    param("initParams", model.InitializationParameters) +
                    helper.NewLine(2)
                );

            var divTag = new TagBuilder("div");
            divTag.WithGeneratedId(blockId)
                .WithAttributes(htmlAttributes)
                .WithInnerHtml(helper.NewLine(2) +
                    objectTag.ToString(TagRenderMode.Normal) +
                    helper.NewLine(1) +
                    historyFrame() +
                    helper.NewLine(1));
            xhtml = divTag.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(xhtml);
        }
    }
}
